package com.xbongbong.paas.help.analysis.data;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.help.UserHelper;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.dto.AnalysisDataDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.parent.parse.data.FormDataUpdateGetAnalysisDataHelp;
import com.xbongbong.pro.user.pojo.ReturnUserAndDepartmentPojo;
import com.xbongbong.sys.domain.entity.DepartmentEntity;
import com.xbongbong.sys.domain.entity.UserEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 关联新建调用的解析方法，暂时使用
 * @author: wufeng
 * @date: 2018/12/11 15:17
 * @desrcption:
 */
@Component
public class FormDataLinkAddUpdateGetAnalysisDataHelp extends FormDataUpdateGetAnalysisDataHelp {
    

    @Override
    public void analysisData(AnalysisDataDTO analysisDataDTO, JSONObject resultJsonObject, Map<String, Object> linkData, Set<Object> linkDataSet) throws XbbException {
        FieldAttrEntity fieldAttrEntity = analysisDataDTO.getFieldAttrEntity();
        String key = analysisDataDTO.getAttr();
        JSONObject objectJsonObject = analysisDataDTO.getDataJsonObject();
        Map<String, UserEntity> userMap = analysisDataDTO.getUserMap();
        Map<String, DepartmentEntity> departmentMap = analysisDataDTO.getDepartmentMap();
        if (FieldTypeEnum.USER_GROUP.getType().equals(fieldAttrEntity.getFieldType())
         || FieldTypeEnum.COUSERID.getType().equals(fieldAttrEntity.getFieldType())
         || FieldTypeEnum.OWNERID.getType().equals(fieldAttrEntity.getFieldType())
        ) {
            List<UserEntity> userList = UserHelper.getUserByKey(key, objectJsonObject, userMap);
            List<ReturnUserAndDepartmentPojo> resultList = new ArrayList<>();
            userList.forEach(item->{
                ReturnUserAndDepartmentPojo returnUserAndDepartmentPojo = new ReturnUserAndDepartmentPojo();
                BeanUtil.copyProperties(item, returnUserAndDepartmentPojo);
                returnUserAndDepartmentPojo.setId(item.getUserId());
                resultList.add(returnUserAndDepartmentPojo);
            });
            resultJsonObject.put(key, resultList);
        } else {
            super.analysisData(analysisDataDTO, resultJsonObject, linkData, linkDataSet);
        }
    }
}
